package com.example.lishop.service.impl

import com.example.lishop.entity.BBusinessEntity
import com.example.lishop.entity.BTestEntity
import com.example.lishop.exception.ExceptEnum
import com.example.lishop.repository.TestRepository
import com.example.lishop.service.TestService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.util.Assert
import java.util.*

/**
 * Created on 2021/3/14 18:14
 * @author shuguo
 */
@Service
class TestServiceImpl : TestService {

    @Autowired
    lateinit var repository: TestRepository

    override fun add(data: BTestEntity): BTestEntity {
        return repository.save(data)
    }

    override fun update(data: BTestEntity) {
        return repository.update(data.id, data.name)
    }

    override fun del(id: Int) {
        repository.del(id)
    }

    override fun list(page:Int, size:Int): Page<BTestEntity> {
        //sort，排序相关的信息，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
        val req: Pageable = PageRequest.of(page, size/*, sort*/)
        return repository.findAll(req)
    }

    override fun detail(id: Int): BTestEntity? {
        val data: Optional<BTestEntity> = repository.findById(id)
        Assert.isTrue(!data.isEmpty, ExceptEnum.NOT_EXIST)
        return if (data.isEmpty) null else data.get()
    }

}